﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Mtbl
{
    public class mtblFunctionDAL:ImtblFunction
    {
        HKEA_CMSEntities model;
        mtblFunction function;
        public mtblFunctionDAL()
        {
            model = DataAccess.GetModelInstance();
        }
        public static mtblFunctionDAL GetInstance()
        {
            return new mtblFunctionDAL();
        }
        public int InsertENT(mtblFunction ent) 
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.mtblFunctions.AddObject(ent);
            return model.SaveChanges();
        }

        public List<mtblFunction> GetListByENT(mtblFunction ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.mtblFunctions.Where(a => a.FunctionUID > 0);
            if (ent.FunctionUID > 0)
                result = result.Where(a => a.FunctionUID == ent.FunctionUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<mtblFunction> GetListByENT(mtblFunction ent)
        {
            var result = model.mtblFunctions.Where(a => a.FunctionUID > 0);
            if (ent.FunctionUID > 0)
                result = result.Where(a => a.FunctionUID == ent.FunctionUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }
        public int GetCountByENT(mtblFunction ent)
        {
            var result = model.mtblFunctions.Where(a => a.FunctionUID > 0);
            if (ent.FunctionUID > 0)
                result = result.Where(a => a.FunctionUID == ent.FunctionUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(mtblFunction ent)
        {
            function = model.mtblFunctions.Where(a=>a.FunctionUID==ent.FunctionUID).FirstOrDefault();
            if (function != null)
            {
                function.Deleted = ent.Deleted;
                function.Function_Name = ent.Function_Name;
                function.LastModifyBy = ent.LastModifyBy;
                function.LastModifyDate = DateTime.Now;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(mtblFunction ent)
        {
            function = model.mtblFunctions.Where(a => a.FunctionUID == ent.FunctionUID).FirstOrDefault();
            if (function != null)
            {
                model.mtblFunctions.DeleteObject(function);
            }
            return model.SaveChanges();
        }
    }
}
